// pages/login/login.js
var util = require('../../utils/util.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    code:"发送验证码",     // 验证码
    register:"绑定手机号",
    login:"注册",
    red:"",
    phone:"",
    codeNamber:"",
    mathCode:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
      return false;
  },
  
  CODE: true,
  // 发送验证码
  code:function(){
      var iNow = 60;
      var that = this ;
      clearInterval(time);
      var re = /^1[34578]\d{9}$/;
      if (!re.test(that.data.phone)) {
        wx.showModal({
          title: '提示',
          content: "请输入正确的手机号码",
          showCancel: false
        })
        return false;
      } else {
     // if (that.CODE){
        var time = setInterval(function () {
          if (iNow == 0) {
            clearInterval(time);
            that.setData({
              code: "发送验证码"
            });
            that.CODE = true;
          } else {
            that.setData({
              code: "剩余" + iNow + "秒"
            });
            iNow--; 
            that.CODE=false;
          }
        }, 1000);

        // 发送验证码
        // if (!(/^1(3|4|5|7|8)\d{9}$/.test(that.data.phone))) {
        //   app.alert('请输入正确的手机号!')
        // } else
        if (app.globalData.openid!=null){
          util.post(app.api.sendCode, {    // 注册
            phone: that.data.phone,
            openid: app.globalData.openid
          }, function (res) {
            if (res.data != -1) {
              that.data.mathCode = res.data.secureCode,
              app.alert('验证已发送，请您及时填写。')
            }
          });
        }
      }
      // else{
      //   console.log(2);
      // }
  },
  //（注册/登录）
  submit:function(){
    var that = this;
    // if (that.data.phone != null&&that.data.phone!=undefined) {
    //   app.alert("请填写手机号码!")
    // }
    // if (that.data.mathCode != that.data.codeNamber){
    //   app.alert("请填写正确的验证码!")
    // }
    if ((/^1(3|4|5|7|8)\d{9}$/.test(that.data.phone)) && that.data.codeNamber != null && that.data.codeNamber != undefined && this.data.mathCode == that.data.codeNamber && that.data.codeNamber !="") {
      //   登录
        util.post(app.api.do_login, {    // 登录
          phone: that.data.phone,
          inputCode:that.data.codeNamber
        }, function (res) {
          if (res.data!=-1) {
            app.globalData.token = true;
            try {    // 同步存储
              wx.setStorageSync('openid', app.globalData.openid)
              wx.setStorageSync('phone', that.data.phone)
            } catch (e) {
            }
            try {
              wx.setStorageSync('token', 'true');
            } catch (e) {
            }
            // console.log(wx.getStorageSync('openid'));
            // console.log(wx.getStorageSync('phone'));
            // console.log(wx.getStorageSync('token'));
            if (wx.getStorageSync('token')=="true"){   // 显示隐藏正常
              if (wx.getStorageSync('openid')){         // 获取到openid
                if (app.globalData.isHaveResume === null) {    // 判断简历
                  wx.showModal({
                    title: '提示',
                    content: '去完善您的简历吧',
                    success: function (res) {
                      if (res.confirm) {
                        wx.navigateTo({
                          url: '/pages/web-view/web-view'
                         // url: '/pages/edit-resume-base/edit-resume-base?type=0',
                        });
                      } else if (res.cancel) {
                      }
                    }
                  })
                } else {
                  wx.reLaunch({
                    url: '/pages/user/index',
                  });
                }
              }
            }
           
          } else {
            app.alert(data.rows);
          }
        })
    }else{
      app.alert("请填写规范的手机号和正确验证码")
    }
  },
  //  手机号失去焦点
  phoneBlur:function(e){
      var value = e.detail.value;
      if (!(/^1(3|4|5|7|8)\d{9}$/.test(value))){
        this.setData({
          red:"1px solid red"
        });
      }else{
        this.setData({
          red: "1px solid #F6F6F6",
          phone: value
        });
      }
  },
  codeBlur:function(e){
    this.setData({
      codeNamber: e.detail.value
    });
  },
  phoneVal: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },
  
  })